
{mfdeclre.i}

DEFINE STREAM bf.

DEFINE VARIABLE i AS INTEGER.

DEFINE VARIABLE pt9-id AS INTEGER.
 
DEFINE VARIABLE str AS CHARACTER FORMAT "x(12)".

DEFINE TEMP-TABLE t1_det NO-UNDO
    FIELD t1_part       LIKE pt8d_part
    FIELD t1_vend       LIKE pc_list
    FIELD t1_pct_adj    LIKE pt8d_pct_adj
    FIELD t1_pct_cal    LIKE pt8d_pct_cal
    INDEX t1_part IS PRIMARY t1_part t1_vend.

DEFINE TEMP-TABLE t2_det NO-UNDO
    FIELD t2_part LIKE pt9e_part
    FIELD t2_pct_adj  LIKE pt9e_pct_adj
    FIELD t2_pct_cal  LIKE pt9e_pct_cal
    INDEX t2_part IS PRIMARY t2_part.

DEFINE VARIABLE price LIKE pc_amt[1].
DEFINE VARIABLE net_prc LIKE pc_amt[1].

DEFINE VARIABLE part LIKE pt9e_part.

DEFINE VARIABLE vend-pct-adj LIKE pt9e_pct_adj.
DEFINE VARIABLE vend-pct-cal LIKE pt9e_pct_adj.
DEFINE VARIABLE part-pct-adj LIKE pt9e_pct_adj.
DEFINE VARIABLE part-pct-cal LIKE pt9e_pct_adj.

pt9-id = 0.
FIND LAST pt9_mstr NO-LOCK NO-ERROR.
IF NOT AVAILABLE pt9_mstr THEN RETURN.
pt9-id = pt9_id.

for each pt7d_det exclusive-lock:
    delete pt7d_det.
end.
FOR EACH pt9d_det NO-LOCK WHERE pt9d_id = pt9_id,
    EACH pt9e_det NO-LOCK WHERE pt9e_id = pt9d_id AND pt9e_group = pt9d_group BREAK BY pt9d_prod BY pt9e_part BY pt9e_sub_part :

    IF FIRST-OF(pt9e_sub_part) THEN DO:
        part-pct-adj = 0.
        part-pct-cal = 0.
    END.

    part-pct-adj = part-pct-adj + pt9e_pct_adj.
    part-pct-cal = part-pct-cal + pt9e_pct_cal.

    IF LAST-OF(pt9e_sub_part) THEN DO:
        CREATE pt7d_det.
        ASSIGN
            pt7d_id = 0
            pt7d_prod = pt9d_prod
            pt7d_part = pt9e_part
            pt7d_sub_part = pt9e_sub_part
            pt7d_pct_adj = part-pct-adj
            pt7d_pct_cal = part-pct-cal.
    END.
END.

EMPTY TEMP-TABLE t1_det.
EMPTY TEMP-TABLE t2_det.

FOR EACH pt9d_det NO-LOCK WHERE pt9d_id = pt9_id,
    EACH pt9e_det NO-LOCK WHERE pt9e_id = pt9d_id AND pt9e_group = pt9d_group BREAK BY pt9e_sub_part BY pt9e_vend :

    IF FIRST-OF(pt9e_sub_part) THEN DO:
        part-pct-adj = 0.
        part-pct-cal = 0.
    END.

    IF FIRST-OF(pt9e_vend) THEN DO:
        vend-pct-adj = 0.
        vend-pct-cal = 0.
    END.

    part-pct-adj = part-pct-adj + pt9e_pct_adj.
    part-pct-cal = part-pct-cal + pt9e_pct_cal.
    vend-pct-adj = vend-pct-adj + pt9e_pct_adj.
    vend-pct-cal = vend-pct-cal + pt9e_pct_cal.

    IF LAST-OF(pt9e_vend) THEN DO:

        CREATE t1_det.
        ASSIGN
            t1_part    = pt9e_sub_part
            t1_vend    = pt9e_vend
            t1_pct_adj = vend-pct-adj
            t1_pct_cal = vend-pct-cal.
    END.

    IF LAST-OF(pt9e_sub_part) THEN DO:

        CREATE t2_det.
        ASSIGN
            t2_part    = pt9e_sub_part
            t2_pct_adj = part-pct-adj
            t2_pct_cal = part-pct-cal.
    END.
END.

for each pt8d_det exclusive-lock:
    delete pt8d_det.
end.

FOR EACH t1_det NO-LOCK, EACH t2_det NO-LOCK WHERE t2_part = t1_part BREAK BY t1_part BY t1_vend:

    CREATE pt8d_det.
    ASSIGN
        pt8d_id   = 0
        pt8d_part = t1_part
        pt8d_vend = t1_vend
        pt8d_pct_adj = IF t2_pct_adj = 0 THEN 0 ELSE ROUND(t1_pct_adj * 100 / t2_pct_adj, 0)
        pt8d_pct_cal = IF t2_pct_cal = 0 THEN 0 ELSE ROUND(t1_pct_cal * 100 / t2_pct_cal, 0).

END.

QUIT.
